package com.yyxh.laiojbackend.judge.codesanbox.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.yyxh.laiojbackend.common.ErrorCode;
import com.yyxh.laiojbackend.exception.BusinessException;
import com.yyxh.laiojbackend.judge.codesanbox.CodeSandbox;
import com.yyxh.laiojbackend.judge.codesanbox.model.ExecuteCodeRequest;
import com.yyxh.laiojbackend.judge.codesanbox.model.ExecuteCodeResponse;

/**
 * 远程沙箱
 */
public class RemoteCodeSandbox implements CodeSandbox {
    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
        System.out.println("远程沙箱");
        String url = "http://139.199.79.244:8090/executeCode";
        String json = JSONUtil.toJsonStr(executeCodeRequest);
        String responseStr = HttpUtil.createPost(url)
                .body(json)
                .execute()
                .body();

        if (StrUtil.isBlank(responseStr)){
            throw new BusinessException(ErrorCode.API_REQUEST_ERROR);
        }
        return JSONUtil.toBean(responseStr, ExecuteCodeResponse.class);
    }
}